package com.edu.common.exception;

import com.edu.common.utils.Result;
import com.edu.common.utils.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 处理全局异常
 */
@ControllerAdvice
public class SystemExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(SystemExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result resoleException(Exception e){
        Result result = new Result(new ResultCode(ResultCode.FAIL,ResultCode.DEFAULT_FAIL_MESSAGE));

        //判断是否为业务异常
        if(e instanceof BusinessException){
            BusinessException businessException = (BusinessException) e;
            if (businessException.getResultCode() !=null){
                result.setResultCode(businessException.getResultCode());
            }
        }
        logger.error("系统异常",e);
        return result;
    }
}
